<?php
namespace cms\modules\v1\models\report;

use cms\modules\common\models\Admin;
use common\models\Functions;
use common\models\Status;
use common\models\user\User;
use yii\base\Exception;
use yii\helpers\ArrayHelper;
use Yii;
/**
 * This is the model class for table "goods".
 *
 * @property int $id
 * @property string $name
 */
class ReportInfo extends \common\models\report\ReportInfo
{
    /**
     * 返回列表数据
     * @param int $page
     * @param int $size
     * @return array
     */
    public function getListData($page = 1, $size = 20) {
        try {

            //搜索条件
            $searchWhere  = $this->getSearch();//自定义搜索条件

            //得到文章的总数
            $count = self::getCountByAndWhere($searchWhere['where'], $searchWhere['andWhere']);
            $data = static::setPageinationAPI($page, $size, $count);

            //设置分页
            $offset = ($data['page'] - 1) * $size;

            //排序 order
            $orderSql = 'id DESC';

            $query = static::getObjectByAndWhere($searchWhere['where'],$searchWhere['andWhere'], '*', $orderSql, $offset, $size);

            $data['data'] = $this->handelList($query);

            return Functions::formatRestAPI(0, '', $data);
        } catch (Exception $e) {
            return Functions::formatRestAPI(1, $e->getMessage());
        }
    }

    /**
     * 自定义参数的搜索  搜索,搜索也分每一列
     * 这里要根据业务逻辑进行修改
     * @return string
     */
    public function getSearch () {
        $where = $andWhere = [];
        $where['is_delete'] = Status::COMMON_IS_NOT_DELETE;
        $search = Yii::$app->request->get();
        if(!empty($search)){
            $phone = isset($search['phone']) ? $search['phone'] : '';
            if($phone) {
                $userAndWhere[] = ['like', "login_name", $phone];
                $where['user_id'] = User::getFormArrayAndWhere([],$userAndWhere,'id','id');
            }
        }
        return [
            'where' => $where,
            'andWhere' => $andWhere
        ];
    }

    /**
     * 进行初始化数据处理
     * @param array $query
     * @return array
     */
    public function handelList($query = []) {
        if (empty($query)) {
            return null;
        }

        $list = $query->with([
            'user' => function($q) {
                $q->select(['id','login_name','nick_name','phone','user_face']);
            },

            'category' => function($q) {
                $q->select(['id','name']);
            },


        ])->asArray()->all();

        foreach ($list as $k => &$v) {
            $singleUserInfo = ArrayHelper::getValue($v,'user',[]);
            $singleUserInfo['user_face'] = $singleUserInfo ? static::getAvatar($singleUserInfo['user_face']) : '';
            $singleCategoryInfo = ArrayHelper::getValue($v,'category',[]);


            $v['user_face'] = $singleUserInfo['user_face'] ?? '';
            $v['user_phone'] = $singleUserInfo['phone'] ?? '';
            $v['user_nick_name'] = $singleUserInfo['nick_name'] ?? '已注销';
            $v['category'] = $singleCategoryInfo ? $singleCategoryInfo['name'] : '';
            $v['time'] = $v['create_time'];
            $v['images'] = static::getAllImages($v['images']);
        }
        return $list;
    }

    public function getInfo($id = 0) {
        try {
            $info = static::getOneInfoById($id);

            if (empty($info)) {
                throw new Exception('未知反馈信息');
            }
            $category = ReportCategory::getOneInfoById($info['category_id']);
            $data['id'] = $info['id'];
            $data['category'] = $category['name'] ?? '';
            $data['content'] = $info['content'];
            $data['images'] = static::getAllImages($info['images']);
            $data['create_time'] = $info['create_time'];

            return Functions::formatRestAPI(0, '', $data);
        } catch (Exception $e) {
            return Functions::formatRestAPI(1, $e->getMessage());
        }
    }


    public function multipleDelete() {
        try {

            $id = \Yii::$app->request->post('id');

            if (empty($id)) {
                throw new Exception("未选择信息");
            }

            $data['is_delete'] = Status::COMMON_IS_DELETE;

            static::updateData($data, ['id' => $id]);
            return Functions::formatRestAPI(0, '删除成功');
        } catch (Exception $e) {
            return Functions::formatRestAPI(1, $e->getMessage());
        }
    }
}
